package com.shuke.sk_worm.tool;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.math.BigDecimal;

public class TimeUtil {

    /**
     * 转换时间，例如：16小时5分55秒 转换成 多少秒
     * @param date
     * @return
     */
    public static String dateConversion(String date){
        if ("-".equals(date) || StringUtils.isBlank(date)){
            return date;
        }
        int second = 0;
        if(StringUtils.isNotBlank(date)){
            int h = date.indexOf("小时");
            int m = date.indexOf("分");
            int s = date.indexOf("秒");
            String hh = "0";
            String mm = "0";
            String ss = "0";
            if(h > -1){
                hh = date.substring(0,h);
                if (m > -1){
                    mm = date.substring(h+2,m);
                    if (s > -1){
                        ss = date.substring(m+1,s);
                    }
                }else{
                    if (s > -1){
                        ss =  date.substring(h+2,s);
                    }
                }
                second = Integer.parseInt(hh) * 3600 + Integer.parseInt(mm ) * 60 + Integer.parseInt(ss);
            }else if(m > -1){
                mm = date.substring(0,m);
                if (s > -1){
                    ss = date.substring(m+1,s);
                }
                second = Integer.parseInt(mm) * 60 + Integer.parseInt(ss);
            }else if(s > -1){
                ss = date.substring(0,s);
                second = Integer.parseInt(ss);
            }

        }
        return String.valueOf(second);
    }

    /**
     * 计算百分比
     * @param percent String
     * @return String
     */
    public static String subPercent(String percent){
        String number = percent.replace("%", "");
        BigDecimal dNumber = new BigDecimal(number);
        BigDecimal hundred = new BigDecimal(100);
        BigDecimal re = hundred.subtract(dNumber);
        return String.valueOf(re) + "%";
    }

}
